package hotel.callback.oversea.controller;

import hotel.base.oversea.constant.MeituanConstants;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.GsonUtils;
import hotel.base.oversea.utils.Result;
import hotel.callback.oversea.service.MtHotelGoodsService;
import hotel.callback.oversea.task.MeituanCheckOneHotelGoodsDataTask;
import hotel.callback.oversea.task.MeituanPartTimeQueryPriceTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 美团酒店产品价格Controller
 * @author qiuyy
 * @date 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/mtHotelGoodsQueryPriceNormal")
@Api(value = "MtHotelGoodsQueryPriceNormalController", tags = {"美团酒店产品价格接口"})
public class MtHotelGoodsQueryPriceNormalController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    MeituanCheckOneHotelGoodsDataTask meituanCheckOneHotelGoodsDataTask;
    @Autowired
    MeituanPartTimeQueryPriceTask meituanPartTimeQueryPriceTask;

    @Autowired
    MtHotelGoodsService mtHotelGoodsService;

    /**
     * 独立价格定时器-普通酒店
     * @return
     */
    @PostMapping("/normalPriceQuery")
    @ApiOperation(value = "独立价格定时器-普通酒店",notes="必须经过auth授权")
    public Result<Object> normalPriceQuery(@RequestBody Map map){
        String timestamp = (String)map.get("timestamp");
        Integer days = (Integer)map.get("days");
        log.info("vipPriceQuery 独立价格定时器-普通酒店 开始执行，countTask={}", GsonUtils.GsonString(map));

        Integer countTask =(Integer) redisTemplate.opsForValue().get("PartTimeQueryCheckPriceVipJob_count_task");
        if(null == countTask){
            countTask =  MeituanConstants.PRICE_QUERY_NORMAL_TASK_COUNT;
        }

        Integer timeHotel = MeituanConstants.PART_PRICE_TIME_HOTEL_COUNT;

        for (int k = 0; k < countTask; k++) {
            // 每一次countTask线程 拿10个酒店上送；内部方法task异步 10个酒店处理15天调用15次；8个异步task共8x15=120次
            List<Long> oneDateHotelIds = new ArrayList<>();
            for (int j = 0; j < timeHotel; j++) {//每次线程10个酒店
                Integer oneDateHotelId = (Integer)redisTemplate.opsForList().rightPop(MeituanConstants.REIDS_QUEUE_NORMAL_PRICE_QUERY);
                if(!ObjectUtils.isEmpty(oneDateHotelId)){
                    oneDateHotelIds.add(Long.valueOf(oneDateHotelId));
                }else{
                    if(null != oneDateHotelIds && oneDateHotelIds.size()>0){
                        meituanPartTimeQueryPriceTask.executeCheckOneHotelGoods("NORMAL",oneDateHotelIds,timestamp+"_"+k+"_"+j,days);
                    }
                    log.info("{} 当前价格coutTask，酒店处理完且酒店队列无数据： k={},j={}",timestamp,k,j);
                    return new Result<>();//return 结束当前
                }
                if(oneDateHotelIds.size()==timeHotel){
                    meituanPartTimeQueryPriceTask.executeCheckOneHotelGoods("NORMAL",oneDateHotelIds,timestamp+"_"+k+"_"+j,days);
                    oneDateHotelIds = new ArrayList<>();
                }
            }
        }
        log.info("vipPriceQuery 独立价格定时器-普通酒店 结束时间 {},countTask={}", DateUtil.getCurrTime(),countTask);

        return new Result();
    }

}
